﻿namespace hedefgrup.graphicsLibrary
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;

    public class trendGraphic : graphic
    {
        private const float BOTTOM = 250f;
        private const float HEIGHT = 208f;
        private const float LEFT = 34f;
        private const int MAP_NAME_TOP = 13;
        private const float RIGHT = 550f;
        private const float TOP = 42f;
        private const float WIDTH = 516f;
        private float X_LABEL_DIFF = 46.90909f;
        public static readonly int[] Y_LABEL_POSITIONS = new int[] { 0x29, 0x53, 0x7d, 0xa7, 0xd1, 0xfc };
        public float[] y2Values;

        public override Bitmap generateImage()
        {
            int num3;
            SizeF ef2;
            Bitmap image = new Bitmap(templates.trendGraphicTemplate);
            Graphics graphics = Graphics.FromImage(image);
            base.yLabels = new string[6];
            SizeF ef = graphics.MeasureString(base.mapNameLines[0], base.mapNameFont);
            graphics.DrawString(base.mapNameLines[0], base.mapNameFont, Brushes.Black, (float) (292f - (ef.Width / 2f)), (float) 13f);
            int num = 0;
            for (float i = base.yMinimumValue; i <= base.yMaximumValue; i += ((float) (base.yMaximumValue - base.yMinimumValue)) / 5f)
            {
                if (num == 6)
                {
                    break;
                }
                base.yLabels[num++] = (i == 0f) ? "0.0" : i.ToString("#.0");
            }
            for (num3 = 0; num3 < 6; num3++)
            {
                ef2 = graphics.MeasureString(base.yLabels[num3], base.yLabelFont);
                graphics.DrawString(base.yLabels[num3], base.yLabelFont, Brushes.Gray, (float) (32f - ef2.Width), (float) (Y_LABEL_POSITIONS[5 - num3] - (ef2.Height / 2f)));
            }
            float minValue = float.MinValue;
            float num5 = float.MinValue;
            float num6 = float.MinValue;
            List<int> list = new List<int>();
            num3 = 0;
            while (num3 < 12)
            {
                if ((base.yValues[num3] > 0f) && (this.y2Values[num3] > 0f))
                {
                    list.Add(num3);
                }
                num3++;
            }
            this.X_LABEL_DIFF = 516f / ((float) (list.Count - 1));
            for (int j = 0; j < list.Count; j++)
            {
                num3 = list[j];
                string text = base.xLabels[num3].Split(new char[] { '|' })[0];
                string str2 = base.xLabels[num3].Split(new char[] { '|' })[1];
                SizeF ef3 = graphics.MeasureString(text, base.xLabelFont);
                SizeF ef4 = graphics.MeasureString(str2, base.xLabelFont);
                graphics.DrawString(text, base.xLabelFont, Brushes.Black, (float) ((34f + (j * this.X_LABEL_DIFF)) - (ef3.Width / 2f)), (float) 255f);
                graphics.DrawString(str2, base.xLabelFont, Brushes.Black, (float) ((34f + (j * this.X_LABEL_DIFF)) - (ef4.Width / 2f)), (float) 266f);
                float num8 = 34f + (j * this.X_LABEL_DIFF);
                float y = 250f - (((base.yValues[num3] - base.yMinimumValue) / ((float) (base.yMaximumValue - base.yMinimumValue))) * 208f);
                if (minValue != float.MinValue)
                {
                    graphics.DrawLine(new Pen(Brushes.Blue, 2f), minValue, num5, num8, y);
                }
                graphics.FillEllipse(Brushes.Blue, (float) (num8 - 3f), (float) (y - 3f), (float) 6f, (float) 6f);
                num5 = y;
                y = 250f - (((this.y2Values[num3] - base.yMinimumValue) / ((float) (base.yMaximumValue - base.yMinimumValue))) * 208f);
                if (minValue != float.MinValue)
                {
                    graphics.DrawLine(new Pen(Brushes.Red, 2f), minValue, num6, num8, y);
                }
                graphics.FillEllipse(Brushes.Red, (float) (num8 - 3f), (float) (y - 3f), (float) 6f, (float) 6f);
                num6 = y;
                minValue = num8;
                ef2 = graphics.MeasureString(base.yValues[num3].ToString("#.0"), base.xLabelFont);
                y = num5 + ((num5 >= num6) ? 5f : (-3f - ef2.Height));
                graphics.DrawString(base.yValues[num3].ToString("#.0"), base.xLabelFont, Brushes.Blue, num8 - (ef2.Width / 2f), y);
                ef2 = graphics.MeasureString(this.y2Values[num3].ToString("#.0"), base.xLabelFont);
                y = num6 + ((num6 > num5) ? 5f : (-3f - ef2.Height));
                graphics.DrawString(this.y2Values[num3].ToString("#.0"), base.xLabelFont, Brushes.Red, num8 - (ef2.Width / 2f), y);
            }
            graphics.Dispose();
            return image;
        }
    }
}

